package com.mg.message.common;

import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class SelectByTimeUtils {
    public static List<String> getBeginAndEndTime(Integer day) {
        if (StringUtils.isEmpty(day)) {
            return null;
        }

        ArrayList<String> BETimes = new ArrayList<>();

        //线程安全的时间工具
        LocalDateTime now = LocalDateTime.now();
        LocalDate nowDay = LocalDate.now();
        LocalDate pastDay = nowDay.minusDays(day);

        DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        String endTime = now.format(dtf1);
        String startTime = pastDay.format(dtf2);

        if (!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)) {
            BETimes.add(startTime);
            BETimes.add(endTime);
        }

        return BETimes;
    }
}
